<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of serviceReportes
 *
 * @author CESAR QUEVEDO
 */
class serviceReportes {

  public function dataReportingTorta($xml)
  {
      $serviceAplication = new serviceAplication();
      $serviceXml        = new serviceXml();
      $diferenciaSegundos = 0;
      $arrayProgramas = array();

      $xmlAplications = $xml->applications->application;

      foreach($xmlAplications as $application)
      {
            $respuesta = $serviceAplication->buscarAplicacion($application->name);
            if(!$respuesta['success'])
            {
               $serviceAplication->addAplicaction($application->name);
            }
            $xmlWindows = $application->windows->window;
            /*recorreger las ventanas para sumar el tiempo total de uso del programa*/
            foreach($xmlWindows as $window)
            {

                 $startTimeWindow = date('Y-m-d H:i:s',strtotime( strval($window->start_time) ) );
                 $endTimeWindow   = date('Y-m-d H:i:s',strtotime( strval($window->end_time  ) ) );
                 $diferenciaSegundos += $serviceXml->diferenciaSegundos($startTimeWindow,$endTimeWindow);
            }
            $respuesta = $serviceAplication->buscarAplicacion($application->name);
            if($respuesta['success'])
            {
                $ventanaActual =  array('nameAplication'  =>$respuesta['object']->getNombrePrograma(), 'timeMinutes'  =>  $diferenciaSegundos/60 );
                array_push($arrayProgramas,$ventanaActual);
                $diferenciaSegundos=0;
            }
      }

            return array("success" => true,"array" => $arrayProgramas);

  }

    public function dataReportingHistoryApplications($xml)
    {

      $xmlAplications = $xml->applications->application;
      $serviceXml        = new serviceXml();
      $arrayAplication = array();
      $arrayDetalleAplication =  array();

      foreach($xmlAplications as $application)
      {

        $xmlWindows = $application->windows->window;
        foreach($xmlWindows as $window)
        {
            $startTimeWindow = date('Y-m-d H:i:s',strtotime( strval($window->start_time) ) );
            $endTimeWindow   = date('Y-m-d H:i:s',strtotime( strval($window->end_time  ) ) );
            $diferenciaSegundos = $serviceXml->diferenciaSegundos($startTimeWindow,$endTimeWindow);
            $arrayDetalleAplication[]=array("title"=>$window->title,"timeMinutes" => $diferenciaSegundos/60);
         }
         $arrayAplication[]      = array("nameAplication" =>$application->name,"arrayDetalle" => $arrayDetalleAplication);
         $arrayDetalleAplication = array();
      }

       return array("success" => true,"array" => $arrayAplication);

    }


  public function dataReportingTableAplication($xml)
  {

      $xmlAplications = $xml->applications->application;
      $serviceXml        = new serviceXml();
      $arrayAplication = array();
      $arrayDetalleAplication =  array();

      foreach($xmlAplications as $application)
      {
        $xmlWindows = $application->windows->window;
        foreach($xmlWindows as $window)
        {
           $xmlHours = $window->hours->hour;
           foreach($xmlHours as $hour)
           {
              $arrayHours[]=array("HoraInicio"=>$hour->start_time,"HoraFin"=>$hour->end_time);
           }
         }
         $arrayAplication[]      = array("nameAplication" =>$application->name,"arrayHoras" => $arrayHours);
         $arrayHours= null;
         $arrayDetalleAplication = array();
      }

       return array("success" => true,"array" => $arrayAplication);
   }


   public function returnColorReportingTable($horaenviada , $arrayHours)
   {

       $success = false;
       $segundos = 0;
       foreach($arrayHours as $row)
       {
           $horaInicioArray  =  date('H',strtotime( $row['HoraInicio'] ) );
           $horaFinArray     =  date('H',strtotime( $row['HoraFin'] ) );

           if($horaInicioArray==$horaenviada)
           {
                $success = true;
                $segundos += $this->porcentajeHora($row['HoraInicio'], $row['HoraFin'], $horaenviada);
           }
           else
           {
               if($horaInicioArray<$horaenviada && $horaFinArray>=$horaenviada)
                {
                   $success = true;
                   $segundos +=  $this->porcentajeHora($row['HoraInicio'], $row['HoraFin'], $horaenviada);
                }
           }
           
       }
      $minutos    = round($segundos/60);
      $porcentaje = ($minutos*100)/60;

      return array("success"=>$success,"porcentaje"=> $porcentaje);
    }

    public function porcentajeHora($horaInicio,$horaFin,$horaAcual)
    {
        $horaInicioArray =  date('H',strtotime(  $horaInicio ) );
        $horaFinArray    =  date('H',strtotime(  $horaFin    ) );

        if($horaInicioArray==$horaAcual)
        {
            $horaInicioReal = date('Y-m-d H:i:s',strtotime( $horaInicio ) );
        }
        else
        {
            $horaInicioAux  = date('Y-m-d H:i:s',strtotime( $horaInicio ) );
            $anio = date("Y",strtotime($horaInicioAux) );
            $mes  = date("m",strtotime($horaInicioAux) );
            $dia  = date("d",strtotime($horaInicioAux) );
            $horaInicioReal = date('Y-m-d H:i:s',strtotime($anio.'-'.$mes.'-'.$dia.' '.$horaAcual.':0:0' ));
        }


        $horaFinAux = $horaAcual+1;
        if($horaFinAux<=$horaFinArray)
        {
            $horaInicioAux  = date('Y-m-d H:i:s',strtotime( $horaInicio ) );
            $anio = date("Y",strtotime($horaInicioAux) );
            $mes  = date("m",strtotime($horaInicioAux) );
            $dia  = date("d",strtotime($horaInicioAux) );
            $horaFinReal = date('Y-m-d H:i:s',strtotime($anio.'-'.$mes.'-'.$dia.' '.$horaFinAux.':0:0' ));
        }
        else
        {
             $horaFinReal  = date('Y-m-d H:i:s',strtotime( $horaFin ) );
        }

        $horaInicioReal = date('H:i:s',strtotime( $horaInicioReal ) );
        $horaFinReal    = date('H:i:s',strtotime( $horaFinReal ) );
        return $this->diferenciaSegundos($horaInicioReal,$horaFinReal);
        
     }

    function diferenciaSegundos($fecha_inicio,$fecha_fin)
    {
        $time_inicio = strtotime($fecha_inicio);
        $time_fin = strtotime($fecha_fin);
        if ($time_inicio>$time_fin)
        {
            return ($time_inicio - $time_fin);
        }
        else
        {
             return ($time_fin - $time_inicio);
        }
    }



}
?>
